package com.javajun.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

/**
 * @ClassName: MainCourse
 * @Description: 主课的实体类，和班级绑定
 * @author: xlm
 * @date: 2018年10月12日
 */
@Entity(name="xlm_maincourse")
public class MainCourse {

	private Integer id;
	// 课程名字
	private String name;
	// 学分
	private Double credit;
	// 学时
	private Double hourse;
	// 学生集合，非持久化，
	private List<Student> students = new ArrayList<>();
	// 班级集合
	private List<Claxx> claxxs = new ArrayList<>();
	// 成绩集合
	private List<MainGrade> mainGrades = new ArrayList<>();
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Column(length=50)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getCredit() {
		return credit;
	}
	public void setCredit(Double credit) {
		this.credit = credit;
	}
	public Double getHourse() {
		return hourse;
	}
	public void setHourse(Double hourse) {
		this.hourse = hourse;
	}
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="mainCourse")
	public List<MainGrade> getMainGrades() {
		return mainGrades;
	}
	public void setMainGrades(List<MainGrade> mainGrades) {
		this.mainGrades = mainGrades;
	}
	
	@ManyToMany(fetch=FetchType.LAZY, mappedBy="mainCourses")
	public List<Claxx> getClaxxs() {
		return claxxs;
	}
	public void setClaxxs(List<Claxx> claxxs) {
		this.claxxs = claxxs;
	}
	
	@Transient
	public List<Student> getStudents() {
		List<Student> students = new ArrayList<>();
		for (Claxx claxx : claxxs) {
			for (Student student : claxx.getStudents()) {
				students.add(student);
			}
		}
		return students;
	}
	
	@Transient
	public Integer getStudentTotal() {
		List<Student> students = new ArrayList<>();
		for (Claxx claxx : claxxs) {
			for (Student student : claxx.getStudents()) {
				students.add(student);
			}
		}
		return students.size();
	}
	
}
